<?php
namespace app\service;

use think\Db;

class UserIntimate
{
    public static function Count($userid, $touserid, $returnScale = false)
    {
        $allpays = 0;
        $overpayscale = 0;
        $uids = [];
        $phone = Db::name('user')->where(['id'=>$userid])->cache(600)->value('phone');
        if (!$phone) {
            $phone = Db::name('recycle_user')->where(['user_id'=>$userid])->cache(600)->value('phone');
        }
        if ($phone) {
            $uids = Db::name('recycle_user')->where(['phone'=>$phone])->cache(600)->column('user_id');
            $line_uid = Db::name('user')->where(['phone'=>$phone])->cache(600)->value('id');
            if ($line_uid) {
                $uids[] = $line_uid;
            }
            if ($uids) {
                $uids[] = $userid;
                $uids = array_unique($uids);
                if (sizeof($uids) <= 1) {
                    $uids = [];
                }
            }
        }
        $device_ids = Db::name('user_repeat_check')->where(['user_id'=>$userid])->cache(120)->value('device_ids');
        if ($device_ids || $uids) {
            $device_users = [];
            $device_userids = [$userid];
            if ($device_ids) {
                $device_ids = explode(',', $device_ids);
                // $uids_2 = Db::name('user_device_records')->where(['device_id'=>['in', $device_ids]])->column('user_id');
                // $uids = array_merge($uids, $uids_2);
                $user_repeat_check_where = [];
                foreach ($device_ids as $device_id) {
                    if (strlen($device_id) < 5) continue;
                    $user_repeat_check_where[] = 'find_in_set("'.$device_id.'",device_ids)';
                }

                if (sizeof($user_repeat_check_where)) {
                    $devusers = Db::name('user_repeat_check')->where(implode(' or ', $user_repeat_check_where))->cache(600)->column('user_id');
                    if ($devusers) {
                        foreach ($devusers as $devuser) {
                            $device_userids[] = $devuser;
                            if ($devuser == $touserid) {
                                continue;
                            }
                            if ($devuser > $touserid) {
                                if (!in_array($touserid . ',' . $devuser, $device_users)) {
                                    $device_users[] = $touserid . ',' . $devuser;
                                }
                            } else {
                                if (!in_array($devuser . ',' . $touserid, $device_users)) {
                                    $device_users[] = $devuser . ',' . $touserid;
                                }
                            }
                        }
                    }
                }
            }
            foreach ($uids as $value) {
                $device_userids[] = $value;
                if ($value == $touserid) {
                    continue;
                }
                if ($value > $touserid) {
                    if (!in_array($touserid . ',' . $value, $device_users)) {
                        $device_users[] = $touserid . ',' . $value;
                    }
                } else {
                    if (!in_array($value . ',' . $touserid, $device_users)) {
                        $device_users[] = $value . ',' . $touserid;
                    }
                }
            }
            $device_users = array_unique($device_users);
            $device_userids = array_unique($device_userids);
            if ($returnScale) {
                // $topupCount = Db::name('user_data')->where(['user_id' => ['in', $device_userids]])->sum('topup');
                // if ($topupCount >= 500) {
                //     $overpayscale = 60;
                //     if ($topupCount >= 15000) {
                //         $overpayscale = 30;
                //     } elseif ($topupCount >= 10000) {
                //         $overpayscale = 50;
                //     }
                // }
            }
            if (!$overpayscale && sizeof($device_users)) {
                $allpays = Db::name('user_intimate')->where(['couple_a' => ['in', $device_users]])->sum('intimate');
            }
        } else {
            if ($returnScale) {
                // $topupCount = Db::name('user_data')->where(['user_id' => $userid])->sum('topup');
                // if ($topupCount >= 500) {
                //     $overpayscale = 60;
                //     if ($topupCount >= 15000) {
                //         $overpayscale = 30;
                //     } elseif ($topupCount >= 10000) {
                //         $overpayscale = 50;
                //     }
                // }
            }
            if (!$overpayscale) {
                if ($userid < $touserid) {
                    $field = 'couple_a';
                } else {
                    $field = 'couple_b';
                }
                $couple = $userid . ',' . $touserid;
                $allpays = Db::name('user_intimate')->where([$field => $couple])->value('intimate');
            }
        }
        if ($returnScale) {
            return 100;
            if ($overpayscale) {
                return $overpayscale;
            }
            if ($allpays >= 5000 * 5) {
                $overpayscale = 30;
            } else if ($allpays >= 3000 * 5) {
                $overpayscale = 50;
            } else if ($allpays >= 1500 * 5) {
                $overpayscale = 70;
            }
            return $overpayscale;
        }
        return $allpays;
    }
    public static function TopupCount($userid)
    {
        $topupCount = 0;
        $device_ids = Db::name('user_repeat_check')->where(['user_id'=>$userid])->cache(600)->value('device_ids');
        $user = Db::name('user')->where(['id'=>$userid])->field('id,name,phone,device_id')->cache(600)->find();


        $uids = [];
        $phone = $user['phone'];
        if (!$phone) {
            $phone = Db::name('recycle_user')->where(['user_id'=>$userid])->cache(600)->value('phone');
        }
        if ($phone) {
            $uids = Db::name('recycle_user')->where(['phone'=>$phone])->cache(600)->column('user_id');
            $line_uid = Db::name('user')->where(['phone'=>$phone])->cache(600)->value('id');
            if ($line_uid) {
                $uids[] = $line_uid;
            }
            if ($uids) {
                $uids[] = $userid;
                $uids = array_unique($uids);
                if (sizeof($uids) <= 1) {
                    $uids = [];
                }
            }
        }


        $device_userids = [];
        if ($device_ids || $uids) {
            $device_userids = [$userid];
            if ($device_ids) {
                $user_repeat_check_where = [];
                $device_ids = explode(',', $device_ids);
                foreach ($device_ids as $device_id) {
                    if (strlen($device_id) < 5) continue;
                    $user_repeat_check_where[] = 'find_in_set("'.$device_id.'",device_ids)';
                }
                if (sizeof($user_repeat_check_where)) {
                    $devusers = Db::name('user_repeat_check')->where(implode(' or ', $user_repeat_check_where))->cache(600)->column('user_id');
                    if ($devusers) {
                        $device_userids = array_merge($device_userids, $devusers);
                    }
                }
            }
            $device_userids = array_merge($device_userids, $uids);
            if ($user && $user['device_id']) {
                $uids = Db::name('user')->where(['device_id'=>$user['device_id']])->cache(120)->column('id');
                $device_userids = array_merge($device_userids, $uids);
            }
            $device_userids = array_unique($device_userids);
            $topupCount = Db::name('user_data')->where(['user_id' => ['in', $device_userids]])->sum('topup');
        } else {
            $device_userids = [$userid];
            if ($user && $user['device_id']) {
                $uid = Db::name('user')->where(['device_id'=>$user['device_id']])->cache(600)->column('id');
                $device_userids = array_unique(array_merge($device_userids, $uid));
            }
            $topupCount = Db::name('user_data')->where(['user_id' => ['in', $device_userids]])->sum('topup');
        }
        return ['user_id'=>$userid, 'topup'=>$topupCount, 'userids'=>implode(',', $device_userids)];
    }
    public static function TopupCount2($userid)
    {
        $topupCount = 0;
        $device_ids = Db::name('user_repeat_check')->master()->where(['user_id'=>$userid])->cache(600)->value('device_ids');
        $user = Db::name('user')->master()->where(['id'=>$userid])->field('id,name,phone,device_id')->cache(600)->find();


        $uids = [];
        $phone = $user['phone'];
        if (!$phone) {
            $phone = Db::name('recycle_user')->master()->where(['user_id'=>$userid])->cache(600)->value('phone');
        }
        if ($phone) {
            $uids = Db::name('recycle_user')->master()->where(['phone'=>$phone])->cache(600)->column('user_id');
            if ($user['phone'] != $phone) {
                $line_uid = Db::name('user')->master()->where(['phone'=>$phone])->cache(600)->value('id');
                if ($line_uid) {
                    $uids[] = $line_uid;
                }
            }
            if ($uids) {
                $uids[] = $userid;
                $uids = array_unique($uids);
                if (sizeof($uids) <= 1) {
                    $uids = [];
                }
            }
        }


        $device_userids = [];
        if ($device_ids || $uids) {
            $device_userids = [$userid];
            if ($device_ids) {
                $device_ids = explode(',', $device_ids);
                $uids_2 = Db::name('user_device_records')->master()->where(['device_id'=>['in', $device_ids]])->cache(600)->column('user_id');
                $uids = array_merge($uids, $uids_2);
            }
            $device_userids = array_unique(array_merge($device_userids, $uids));
            $topupCount = Db::name('user_data')->master()->where(['user_id' => ['in', $device_userids]])->cache(60)->sum('topup');
        } else {
            $device_userids = [$userid];
            if ($user && $user['device_id']) {
                $uid = Db::name('user')->master()->where(['device_id'=>$user['device_id']])->cache(600)->column('id');
                $device_userids = array_unique(array_merge($device_userids, $uid));
            }
            $topupCount = Db::name('user_data')->master()->where(['user_id' => ['in', $device_userids]])->cache(60)->sum('topup');
        }
        return ['user_id'=>$userid, 'topup'=>$topupCount, 'userids'=>implode(',', $device_userids)];
    }
}
